package org.bigmarketapi.gatewaycenter;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.bigmarketapi.gatewaycenter.domain.manage.service.IManageService;
import org.bigmarketapi.gatewaycenter.domain.registry.model.aggre.appRegistryAggregate;
import org.bigmarketapi.gatewaycenter.domain.registry.model.vo.AppInterfaceMethodVO;
import org.bigmarketapi.gatewaycenter.domain.registry.service.IRegistry;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.common.Result;
import org.bigmarketapi.gatewaycenter.infrastructure.persistent.po.GwServerDetail;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.Map;

@SpringBootTest
@Slf4j
class GatewayCenterApplicationTests {
    @Resource
    IManageService manageService;
    @Resource
    IRegistry registry;

    @Test
    void contextLoads() {
        manageService.registerGatewayServerNode("10001", "api-gateway-g1", "电商支付网关", "127.0.0.196");
    }

    @Test
    void test_registry() {
        AppInterfaceMethodVO applicationInterfaceVO01 = new AppInterfaceMethodVO();
        applicationInterfaceVO01.setSystemId("api-gateway-test");
        applicationInterfaceVO01.setInterfaceId("cn.bugstack.gateway.rpc.IActivityBooth");
        applicationInterfaceVO01.setMethodId("sayHi");
        applicationInterfaceVO01.setMethodName("测试方法");
        applicationInterfaceVO01.setParameterType("java.lang.String");
        applicationInterfaceVO01.setUri("/wg/activity/sayHi");
        applicationInterfaceVO01.setHttpType("GET");
        applicationInterfaceVO01.setAuth(0);
        registry.registerApplicationInterfaceMethod(applicationInterfaceVO01);

        AppInterfaceMethodVO applicationInterfaceVO02 = new AppInterfaceMethodVO();
        applicationInterfaceVO02.setSystemId("api-gateway-test");
        applicationInterfaceVO02.setInterfaceId("cn.bugstack.gateway.rpc.IActivityBooth");
        applicationInterfaceVO02.setMethodId("insert");
        applicationInterfaceVO02.setMethodName("插入方法");
        applicationInterfaceVO02.setParameterType("cn.bugstack.gateway.rpc.dto.XReq");
        applicationInterfaceVO02.setUri("/wg/activity/insert");
        applicationInterfaceVO02.setHttpType("POST");
        applicationInterfaceVO02.setAuth(1);
        registry.registerApplicationInterfaceMethod(applicationInterfaceVO02);
    }

    @Test
    void test_aggregate() {
        final appRegistryAggregate aggregate = registry.queryAppRegistry("api-gateway-g4");
        log.info("aggregate: {}", JSON.toJSONString(aggregate));
    }

    @Test
    void test_http_post() {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("groupId", "10001");
        paramMap.put("gatewayId", "api-gateway-g4");
        paramMap.put("gatewayName", "电商支付网关");
        paramMap.put("gatewayAddress", "127.0.0.196");
        final String post = HttpUtil.post("http://127.0.0.1:9099/wg/admin/registerGateway/registerGatewayServerNode", paramMap);
        log.info("post: {}",post);
        final Result<Boolean> booleanResult = JSON.parseObject(post, new TypeReference<Result<Boolean>>() {
        });
        log.info("{}",booleanResult);
    }
}
